﻿using LCL.Config;
using LCL.LData;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace LCL.DbSqlMonitor
{
    public class SQLServerImpl : BaseImpl, ISQLMonitor
    {
        public SQLServerImpl(DbConfigInfo dbConfig)
            : base(dbConfig)
        {
            
        }
        public DataTable GetSqlALL()
        {
            string sql =string.Format(@"SELECT t.spid, t.kpid, t.blocked, t.status, t.lastwaittype, t.waitresource, t.waittime/1000 waittime, t.last_batch, DB_NAME(t.dbid) DbName, t.loginame, t.program_name, t.hostname, t.hostprocess, dc.text FROM master.sys.sysprocesses t OUTER APPLY master.sys.dm_exec_sql_text(t.sql_handle) dc WHERE t.spid >= {0} ",this.RowNum);

            return _dba.ExecuteDataTable(sql);
        }
        public bool DbLinkTest()
        {
            bool flg = true;
            try
            {
                string sql = "SELECT getdate()";
                _dba.ExecuteNonQuery(sql, null);
                flg = true;
            }
            catch (Exception e)
            {
                flg = false;
                Logger.Error("DbLinkTest:", e);
            }
            return flg;
        }
        public bool Clear()
        {
            return true;
        }
    }
}
